<template>
  <div class="login">
      <div>用户名：<input type="text" v-model="username" /></div>
      <div>密码&nbsp;：<input type="password" @keyup.enter="Login" v-model="password" /></div>
      <div style="margin-right: 0;text-align: center;">
          <button class="login-button" style="margin-right:8%;"  v-on:click="Login">登录</button>
          <button class="login-button">注册</button>
      </div>
  </div>
</template>

<script>
export default {
  data () {
      return {
          username:'',
          password:''
      }
  },
  created:function () {
      document.title = '登录';
  },
  methods:{
      //登录
      Login: function(){
          let self = this;
          self.$lay.loading(1,0);
          let form = new FormData();
          form.append('type',1);
          form.append('name',self.username);
          form.append('pass',self.password);
          form.append('go','登录');
          self.$http.post('q.php/user.login.json?u=index.index.json&_origin=*',form).then(function(res){
              if(res.status == 200){
                  console.log(res);
                  if(res.data.success){
                      self.$lay.loading(-1);
                      self.$lay.toast('登录成功！',1000);

                      let sid = res.data.sid;
                      var Days = 30;
                      var exp = new Date();
                      exp.setTime(exp.getTime() + Days*24*60*60*1000);
                      document.cookie = 'sid='+sid+';'+exp.toGMTString();
                      g.sid = sid;

                      setTimeout(function(){
                        window.location.href = '/';
                      }, 1000);
                    //   alert('');
                      
                      
                  }else{
                      alert('登录失败！');
                  }
              }
          }).catch(function(e){
              alert('老虎那边的服务器出现错误了，稍后再试！');
              console.log('出错了');
              console.log(e);
          });
      }
  }
}
</script>
<style scoped>
</style>
